package com.example.administrator.music;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.IBinder;

public class MusicService extends Service {
    private MediaPlayer player;
    private boolean isPause=false;
    public MusicService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        initMediaPlayer();


    }
    private void initMediaPlayer() {
        player = new MediaPlayer();
        if(!isPause) {

            try {
                player.reset();
                player.setDataSource(getMusicPath("choubaguai.mp3"));
                player.prepare();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action=intent.getStringExtra("action");
        switch (action){
            case "play":
                play();
                break;
            case "pause":
                pause();
                break;
        }
        return super.START_REDELIVER_INTENT;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return new Progressor(player);
    }
    private  String getMusicPath(String songNmae){
        String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/mp3/"+songNmae;
        return path;
    }
    public void play(){

        player.start();
        isPause=false;
    }
    public void pause(){
        player.pause();
        isPause=true;
    }
}
